﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
namespace LightSwitchApplication
{
    public partial class SearchFilteredShops
    {
        partial void SearchFilteredShops_InitializeDataWorkspace(List<IDataService> saveChangesTo)
        {
            if (this.Application.User.HasPermission(Permissions.CanEditShop))//admin
            {

            }
            else if (this.Application.User.HasPermission(Permissions.CanSearchShop) && this.Application.User.HasPermission(Permissions.CanManageMyShop))
            {
                string userName = this.Application.User.FullName;
                //sales person
                foreach (SalesPerson item in this.DataWorkspace.ApplicationData.SalesPersons)
                {
                    if (item.aspnet_Profile != null && item.aspnet_Profile.PropertyValuesString == userName)
                    {
                        this.SelectedSalesPerson = item;
                        break;
                    }
                }

                //customer
                foreach (Customer item in this.DataWorkspace.ApplicationData.Customers.Where(c => c.aspnet_Profile_UserId != null))
                {
                    if (item.aspnet_Profile != null && item.aspnet_Profile.PropertyValuesString == userName)
                    {
                        this.SelectedCustomer = item;
                        break;
                    }
                }
            }
            else {
                this.FindControl("Search").IsReadOnly = true;
            }
        }

        partial void SearchFilteredShops_Activated()
        {
            try
            {
                this.FindControl("SelectedSalesPersion").IsEnabled = (this.SelectedSalesPerson == null);
                this.FindControl("SelectedCustomer").IsEnabled = (this.SelectedCustomer == null);
            }
            catch (Exception)
            {
            }
        }

        partial void Search_Execute()
        {
            FilteredShops.Load();
        }

        partial void ClearFilter_Execute()
        {
            if (!this.FindControl("SelectedSalesPerson").IsReadOnly)
            {
                this.SelectedSalesPerson = null;
            }

            if (!this.FindControl("SelectedCustomer").IsReadOnly)
            {
                this.SelectedCustomer = null;
            }

            this.SelectedShopName = null;

            if (!this.FindControl("SelectedShopArea").IsReadOnly)
            {
                this.SelectedShopArea = null;
            }

            if (!this.FindControl("SelectedShopDoorArea").IsReadOnly)
            {
                this.SelectedShopDoorArea = null;
            }

            if (!this.FindControl("SelectedShopType").IsReadOnly)
            {
                this.SelectedShopType = null;
            }

            if (!this.FindControl("SelectedShopCategory").IsReadOnly)
            {
                this.SelectedShopCategory = null;
            }

            if (!this.FindControl("SelectedShopState").IsReadOnly)
            {
                this.SelectedShopState = null;
            }
        }

        partial void Cancel_Execute()
        {
            this.Close(false);
        }
    }
}
